package com.wisnews.controller.sec;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wisnews.pojo.Input;
import com.wisnews.pojo.Result;
import com.wisnews.pojo.sec.entity.Permission;
import com.wisnews.service.sec.Impl.PermissionServiceImpl;
import com.wisnews.util.Constants;
import com.wisnews.util.MenuTreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 系统权限信息 前端控制器
 * </p>
 *
 * @author yuyd
 * @since 2020-12-20
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private PermissionServiceImpl permissionService;

    @RequestMapping("autoGenMenu")
    public Result autoGenMenu() {
        MenuTreeNode root = permissionService.getMenuInfo();
        return new Result<>(Constants.SYS_CODE.SUCCESS, Constants.SYS_CODE.SUCCESS_MESSAGE,root);
    }

    @RequestMapping("getPermission")
    public Result getAllRequest(@RequestBody Input<Permission> input) {
        IPage page =  permissionService.getPermissionForPage(input.getReq(),input.toPage());
        return new Result(Constants.SYS_CODE.SUCCESS,Constants.SYS_CODE.SUCCESS_MESSAGE,page);
    }

    @RequestMapping("getAllPermission")
    public Result getAllRequest() {
        List<Permission> list = permissionService.list();
        return new Result(Constants.SYS_CODE.SUCCESS,Constants.SYS_CODE.SUCCESS_MESSAGE,list);
    }

    @RequestMapping("/save")
    public Result getAllRequest(@RequestBody Permission permission) {

        Result result = new Result(Constants.SYS_CODE.SUCCESS, Constants.SYS_CODE.SUCCESS_MESSAGE);
        if (permissionService.save(permission)){
            return result;
        }
        result.setCode(Constants.SYS_CODE.FAIL);
        result.setMsg(Constants.SYS_CODE.FAIL_MESSAGE);
        return result;

    }

    @RequestMapping("/delete")
    public Result deletePermission(@RequestBody List<String> idList) {
        Result result = new Result(Constants.SYS_CODE.SUCCESS, Constants.SYS_CODE.SUCCESS_MESSAGE);
        if (permissionService.removeByIds(idList)){
            return result;
        }
        result.setCode(Constants.SYS_CODE.FAIL);
        result.setMsg(Constants.SYS_CODE.FAIL_MESSAGE);
        return result;
    }
}
